// utils/util.js

/**
 * 格式化时间
 * @param {Date} date 日期对象
 * @param {string} format 格式字符串，如 'yyyy-MM-dd hh:mm:ss'
 * @return {string} 格式化后的时间字符串
 */
function formatDate(date, format) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return format.replace(/yyyy/, year.toString())
    .replace(/MM/, (month < 10 ? '0' + month : month).toString())
    .replace(/dd/, (day < 10 ? '0' + day : day).toString())
    .replace(/hh/, (hour < 10 ? '0' + hour : hour).toString())
    .replace(/mm/, (minute < 10 ? '0' + minute : minute).toString())
    .replace(/ss/, (second < 10 ? '0' + second : second).toString())
}
/**
 * 将时间字符串转换为秒数
 * @param {string} timeStr - 时间字符串，如 "2h"、"3d"、"1.5h 30m"
 * @returns {number} 对应的秒数
 */
function timeToSeconds(timeStr) {
  if (!timeStr) return ''
  const unitMap = {
    s: 1, // 秒
    m: 60, // 分钟
    h: 3600, // 小时
    d: 86400, // 天
    w: 604800 // 周
  };

  // 匹配数字和单位，支持小数和空格分隔
  const matches = timeStr.matchAll(/(\d+\.?\d*)\s*([smhdw])/gi);
  let seconds = 0;

  for (const match of matches) {
    const value = parseFloat(match[1]);
    const unit = match[2].toLowerCase();
    seconds += value * (unitMap[unit] || 0);
  }

  return seconds;
}
/**
 * 节流函数
 * @param {Function} fn 需要节流的函数
 * @param {number} delay 延迟时间(ms)
 * @return {Function} 节流后的函数
 */
function throttle(fn, delay) {
  let timer = null
  return function () {
    if (timer) return
    timer = setTimeout(() => {
      fn.apply(this, arguments)
      timer = null
    }, delay)
  }
}

function getSystemInfo() {
  return new Promise((resolve, reject) => {
    wx.getSystemSetting({
      success: res => resolve(res),
      fail: err => reject(err)
    })
  })
}
// --防抖函数
function debounce(fn, delay) {
  let timer = null
  return function () {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, arguments)
      timer = null
    }, delay)
  }
}

// --页面统一跳转
function navigateToPage(e) {
  const {
    url
  } = e.currentTarget.dataset;
  console.log(url)
  // --后续控制逻辑
  wx.navigateTo({
    url
  })
}

function getRandomInt(max) {
  return Math.floor(Math.random() * max)
}

function generateGridList(childCount, columns) {
  const ans = []
  for (let i = 0; i < childCount; i++) {
    ans.push({
      id: i,
      sub: getRandomInt(columns) + 1,
      image: `http://localhost:3000/uploads/images/products/${i}.jpg`
    })
  }
  return ans
}

function checkPathHasTabbar(params) {
  if (!params) return false;
  let tabBars = ['home', 'DIY', 'pets', 'mine'];
  const hasTabBar = tabBars.some(tab => params.includes(tab)); // 或者 params.indexOf(tab) > -1

  return !hasTabBar; // 如果包含 tabBars 项，返回 false；否则返回 true
}

function getHeaderHeight() {
  const windowInfo = wx.getWindowInfo();
  const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  console.log(windowInfo,menuButtonInfo)
  const statusBarHeight = windowInfo.statusBarHeight;
  const navBarHeight = (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height;
  
  return {
    statusBarHeight,    // 状态栏高度
    navBarHeight,       // 导航栏高度
    headerHeight: statusBarHeight + navBarHeight, // 总高度
    menuButtonInfo      // 胶囊按钮信息
  };
}
// 导出模块
module.exports = {
  formatDate,
  timeToSeconds,
  throttle,
  debounce,
  navigateToPage,
  generateGridList,checkPathHasTabbar,
  getHeaderHeight
}